package com.ldxiwxr.controller.common;

import com.ldxiwxr.common.utils.KaptchaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证码控制类
 *
 * @author SuBanxia
 */
@Api(tags = "/KaptchaController")
@RestController
public class KaptchaController {

    @Resource
    private KaptchaUtils kaptchaUtils;

    @ApiOperation(value = "返回验证码")
    @GetMapping(value = "/kaptcha", produces = "image/jpeg")
    public void kaptcha(HttpServletResponse response) throws IOException {
        //定义response输出类型为image/jpeg类型
        response.setDateHeader("Expires", 0);
        //standard HTTP/1.1 no-cache headers .
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        //IE extended HTTP/1.1 no-cache headers (use addHeader)
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        //standard HTTP/1.0 no-cache header .
        response.setHeader("Pragma", "no-cache");
        //设置返回编码类型图片
        response.setContentType("image/jpeg");
        kaptchaUtils.kaptchaProduce(response);
    }
}
